package com.yimi.yinhepda.views.main.my_module;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.pda.ym.callbacks.UICallBack;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.db.dao.LineVoTableDao;
import com.yimi.yinhepda.db.dao.StorageInfoTableDao;
import com.yimi.yinhepda.db.dao.UCDeptTableDao;
import com.yimi.yinhepda.db.table.LineVoTable;
import com.yimi.yinhepda.db.table.StorageInfoTable;
import com.yimi.yinhepda.downLoadBaseData.DownLoadBaseData;
import com.yimi.yinhepda.downLoadBaseData.DownLoadForOffLineData;
import com.yimi.yinhepda.entity.request.QueryStorageInfoRequest;
import com.yimi.yinhepda.entity.response.QueryStorageInfoResponse;
import com.yimi.yinhepda.utils.ToastUtil;
import com.yimi.yinhepda.base.BaseInitActivity;
import com.yimi.yinhepda.views.stockCheck_module.StockCheckUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author: lj
 * Date: 2019/10/24 15:45
 * Description:基础数据界面
 */
public class BaseDataActivity extends BaseInitActivity {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    /**
     * 出发线路条数
     */
    @Bind(R.id.tv_set_out_num)
    TextView tvSetOutNum;
    /**
     * 到达线路条数
     */
    @Bind(R.id.tv_arrive_num)
    TextView tvArriveNum;
    /**
     * 货区条数
     */
    @Bind(R.id.tv_fenbo_storage_area)
    TextView tvStorageNum;

    /**
     * 下一站集合
     */
    private long mNextDeptCount;
    /**
     * 上一站集合
     */
    private long mPreviousDeptCount;
    /**
     * 货区集合
     */
    private List<StorageInfoTable> mStorageInfoTableList;

    @Override
    public String getPageName() {
        return "基础数据界面";
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_base_data;
    }

    @Override
    public void initView(Bundle savedInstanceState) {

    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initData() {
        tvTitle.setText("基础数据");
        //查询下一站条数
        queryNextDept();
        //查询上一站条数
        queryPreviousDept();
        //查询当前部门货区条数
        queryStorageInfos();
    }

    @OnClick({R.id.iv_back, R.id.ll_download_data, R.id.ll_set_out, R.id.ll_arrive, R.id.ll_storage_area})
    @Override
    protected void onClickBtn(View v) {
        super.onClickBtn(v);
        switch (v.getId()) {
            case R.id.iv_back:
                //返回
                finish();
                break;
            case R.id.ll_download_data:
                //数据下载
                if (!sharedPreferUtil.getOfflineMode()) {
                    //下载基础数据
                    new DownLoadBaseData(this, true).downloadBaseData();
                    //下载月台号和装卸组数据、部门所有用户
                    new DownLoadForOffLineData(this).startDownLoadPlatformNoData();
                } else {
                    ToastUtil.showToast("离线模式不可下载数据！");
                }
                break;
            case R.id.ll_set_out:
                //出发线路
                toActivity(SetOutLineActivity.class);
                break;
            case R.id.ll_arrive:
                //到达线路
                toActivity(ArriveLineActivity.class);
                break;
            case R.id.ll_storage_area:
                //分拨货区
                toActivity(StorageInfoActivity.class);
                break;
            default:
                break;
        }
    }

    /**
     * 查询下一站
     */
    private void queryNextDept() {
        Observable.create(new Observable.OnSubscribe<Void>() {
            @Override
            public void call(Subscriber<? super Void> subscriber) {
                //查询出发线路下一站
                mNextDeptCount = queryNextDeptCount(sharedPreferUtil.getDeptCode());
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Void>() {
            @Override
            public void onCompleted() {
                if (tvSetOutNum!=null){
                    tvSetOutNum.setText(String.format("%s条", mNextDeptCount));
                }
            }

            @Override
            public void onError(Throwable throwable) {
            }

            @Override
            public void onNext(Void aVoid) {
            }
        });
    }

    /**
     * 查询上一站
     */
    private void queryPreviousDept() {
        Observable.create(new Observable.OnSubscribe<Void>() {
            @Override
            public void call(Subscriber<? super Void> subscriber) {
                //查询出发线路上一站
                mPreviousDeptCount = queryPreviousDeptCount(sharedPreferUtil.getDeptCode());
                subscriber.onCompleted();
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<Void>() {
            @Override
            public void onCompleted() {
                if (tvArriveNum != null) {
                    tvArriveNum.setText(String.format("%s条", mPreviousDeptCount));
                }
            }

            @Override
            public void onError(Throwable throwable) {
            }

            @Override
            public void onNext(Void aVoid) {
            }
        });
    }

    /**
     * 查询货区
     */
    private void queryStorageInfos() {
        QueryStorageInfoRequest request = new QueryStorageInfoRequest();
        request.setDeptCode(sharedPreferUtil.getDeptCode());
        StockCheckUtil.queryStorageInfo(request, new UICallBack() {
            @Override
            public void onError(String msg) {
                showToast(msg);
            }

            @Override
            public void onSuccess(Object obj) {
                QueryStorageInfoResponse response = (QueryStorageInfoResponse) obj;
                if (response.getData() != null) {
                    tvStorageNum.setText(String.format("%s条", response.getData().size()));
                } else {
                    tvStorageNum.setText("0条");
                }
            }
        });
    }

    /**
     * 查询下一站总条数
     */
    private long queryNextDeptCount(String deptCode) {
        //查询下一站线路
        List<LineVoTable> lineList = new LineVoTableDao().findSourceZoneCode(deptCode);
        //查询优速网点数据总条数
        long ucDeptCount = new UCDeptTableDao().queryDataAllCount();
        return (lineList != null ? lineList.size() : 0) + ucDeptCount;
    }

    /**
     * 查询上一站总条数
     */
    private long queryPreviousDeptCount(String deptCode) {
        //查询上一站线路
        List<LineVoTable> lineList = new LineVoTableDao().findDestZoneCode(deptCode);
        //查询优速网点数据总条数
        long ucDeptCount = new UCDeptTableDao().queryDataAllCount();
        return (lineList != null ? lineList.size() : 0) + ucDeptCount;
    }
}
